package com.fesine.quartz.chapter8;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.spi.SchedulerPlugin;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/11/20 15:10
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/20 15:10
 */
public class ParentPlugin implements SchedulerPlugin {
    private static Log logger = LogFactory.getLog(ParentPlugin.class);

    //子插件列表
    private List childPlugins = new ArrayList();

    //
    private String childPluginNames;

    private String pluginName;

    private Scheduler scheduler;

    public ParentPlugin() {
    }

    @Override
    public void initialize(String name, Scheduler scheduler) throws SchedulerException {
        this.pluginName= name;
        this.scheduler = scheduler;

        logger.info("Searching for child plugins to load");
        //通过逗号分隔字符串
        StringTokenizer tokenizer = new StringTokenizer(childPluginNames, ",");
        //遍历
        while (tokenizer.hasMoreElements()) {
            String pluginClassName = tokenizer.nextToken();
            try {
                //通过配置的类名转换成类
                Class pluginClass = Class.forName(pluginClassName);
                Object obj = pluginClass.newInstance();
                //判断是不是插件子类
                if (obj instanceof SchedulerPlugin) {
                    //转成插件类
                    SchedulerPlugin childPlugin = (SchedulerPlugin) obj;
                    logger.info("Init child Plugin " +
                            pluginClassName);
                    //注册子插件
                    childPlugin.initialize(pluginClassName, scheduler);
                    //添加子插件到列表中
                    childPlugins.add(childPlugin);

                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void start() {
        int size = childPlugins.size();
        for (int i = 0; i < size; i++) {
            //获取子插件，启动
            SchedulerPlugin childPlugin = (SchedulerPlugin) childPlugins.get(i);
            logger.info("Starting Child Plugin " + childPlugin);
            childPlugin.start();
        }
    }

    @Override
    public void shutdown() {
        int size = childPlugins.size();
        for (int i = 0; i < size; i++) {
            SchedulerPlugin childPlugin =
                    ((SchedulerPlugin) childPlugins.get(i));

            logger.info("Stopping Plugin " + childPlugin);
            childPlugin.shutdown();
        }

    }

    public String getChildPluginNames() {
        return childPluginNames;
    }

    public void setChildPluginNames(String childPluginNames) {
        this.childPluginNames = childPluginNames;
    }

    public String getPluginName() {
        return pluginName;
    }

    public void setPluginName(String pluginName) {
        this.pluginName = pluginName;
    }
}
